package com.sdk.util;

import android.content.Intent;
import android.os.Bundle;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Set;

import dalvik.system.DexClassLoader;

public class CommonUtils {
    private static String Tag = "CommonUtils";


    public static byte[] getContent(String filePath){
        try {
            File file = new File(filePath);
            long fileSize = file.length();
            if (fileSize > Integer.MAX_VALUE) {
                System.out.println("file too big...");
                return null;
            }
            FileInputStream fi = new FileInputStream(file);
            byte[] buffer = new byte[(int) fileSize];
            int offset = 0;
            int numRead = 0;
            while (offset < buffer.length
                    && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
                offset += numRead;
            }
            // 确保所有数据均被读取
            if (offset != buffer.length) {
                throw new IOException("Could not completely read file "
                        + file.getName());
            }
            fi.close();
            return buffer;
        }catch (Exception e){
            return null;
        }
    }



    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static String execShell(String cmd, boolean waitfor) {
        StringBuilder sb = new StringBuilder();
        try {
            LogUtils.d(Tag, "execShell: " + cmd);

            ProcessBuilder pb = new ProcessBuilder("su");
            pb.redirectErrorStream(true);
            Process p = null;
            BufferedReader br = null;
            try {
                p = pb.start();
                //获取输出流
                OutputStream outputStream = p.getOutputStream();
                DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                //将命令写入
                dataOutputStream.writeBytes(cmd);
                //提交命令
                dataOutputStream.flush();
                //关闭流操作
                dataOutputStream.close();
                outputStream.close();
                if (waitfor) {
                    br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line = null;
                    //LogUtils.d(Tag, "wait for shell return start.");
                    while (true) {
                        //LogUtils.d(Tag, "br.readLine start.");
                        line = br.readLine();
                        //LogUtils.d(Tag, "br.readLine end.");
                        if (line != null) {
                            //LogUtils.d(Tag, "cmd result: " + line);
                            if (line.indexOf("WARNING:") < 0) {
                                if (sb.length() > 0) {
                                    sb.append("\r\n");
                                }
                                sb.append(line);
                            }
                        } else {
                            break;
                        }

                    }
                    p.waitFor();
                    if (p != null) {
                        p.destroy();
                    }
                }
            } finally {
                if (br != null) {
                    br.close();
                }

            }
        } catch (Exception e) {
            LogUtils.e(Tag, "execShell");
        }
        LogUtils.d(Tag, "execShell: " + cmd + " end>>" + sb.toString());
        return sb.toString();
    }

    public static String execShellSh(String cmd, boolean waitfor) {
        StringBuilder sb = new StringBuilder();
        try {
            LogUtils.d(Tag, "execShell: " + cmd);

            ProcessBuilder pb = new ProcessBuilder("sh");
            pb.redirectErrorStream(true);
            Process p = null;
            BufferedReader br = null;
            try {
                p = pb.start();
                //获取输出流
                OutputStream outputStream = p.getOutputStream();
                DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                //将命令写入
                dataOutputStream.writeBytes(cmd);
                //提交命令
                dataOutputStream.flush();
                //关闭流操作
                dataOutputStream.close();
                outputStream.close();
                if (waitfor) {
                    br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line = null;
                    //LogUtils.d(Tag, "wait for shell return start.");
                    while (true) {
                        //LogUtils.d(Tag, "br.readLine start.");
                        line = br.readLine();
                        //LogUtils.d(Tag, "br.readLine end.");
                        if (line != null) {
                            //LogUtils.d(Tag, "cmd result: " + line);
                            if (line.indexOf("WARNING:") < 0) {
                                if (sb.length() > 0) {
                                    sb.append("\r\n");
                                }
                                sb.append(line);
                            }
                        } else {
                            break;
                        }

                    }
                    p.waitFor();
                    if (p != null) {
                        p.destroy();
                    }
                }
            } finally {
                if (br != null) {
                    br.close();
                }

            }


        } catch (Exception e) {
            LogUtils.e(Tag, "execShell");
        }
        LogUtils.d(Tag, "execShell: " + cmd + " end>>" + sb.toString());
        return sb.toString();
    }

    public static void printIntent(Intent intent) {
        if (intent == null) {
            return;
        }
        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            return;
        }
        Set<String> keySet = bundle.keySet();
        for (String key : keySet) {
            LogUtils.d(Tag, "key: " + key + " = " + String.valueOf(bundle.get(key)));
        }
    }

    public static void printCallStatck() {
        Throwable ex = new Throwable();
        DexClassLoader dexClassLoader;
        StackTraceElement[] stackElements = ex.getStackTrace();
        if (stackElements != null) {
            for (int i = 0; i < stackElements.length; i++) {
                LogUtils.d(Tag, stackElements[i].getClassName());
//                Log.d(Tag, stackElements[i].getFileName());
                LogUtils.d(Tag, stackElements[i].getLineNumber() + "/t");
                LogUtils.d(Tag, stackElements[i].getMethodName());
                LogUtils.d(Tag, "-----------------------------------");
            }
        }
    }

    public static void writeStringToFile(String filePath, String content) {
        try {
            File file = new File(filePath);
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.println(content);// 往文件里写入字符串
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static String readStringFromFile(String filePath){
        try {
            InputStream is = new FileInputStream(filePath);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            return reader.readLine();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "0";
    }

}
